package com.example.mytips.bottomWindow;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.intellij.ui.jcef.JBCefApp;
import com.intellij.ui.jcef.JBCefBrowser;
import org.cef.browser.CefBrowser;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;


///实现这个类ToolWindowFactory，就是边边的窗口
public class WebPlusWindow implements ToolWindowFactory {

    public List<WebPlusEntity> getUrl() {
        List<WebPlusEntity> list = new ArrayList<>();
        list.add(WebPlusEntity.getTenXunDocument());
        list.add(WebPlusEntity.getTenXunYuanBao());
        list.add(WebPlusEntity.getGoogle());

        return list;
    }

    private ToolWindow toolWindow;

    @Override
    public void createToolWindowContent(Project project, ToolWindow toolWindow) {
        this.toolWindow = toolWindow;
        if (!JBCefApp.isSupported()) {
            System.out.println("不支持");
            return;
        }

        List<WebPlusEntity> urls = getUrl();
        for (WebPlusEntity entity : urls) {
            contentItem(entity.displayName, entity.url);
        }

    }

    ///一个标签页
    public JPanel contentItem(String displayName, String url) {
        //垂直方向排列
        JPanel bodyPanel = new JPanel(new BorderLayout());
        bodyPanel.setLayout(new BoxLayout(bodyPanel, BoxLayout.Y_AXIS));

        JBCefBrowser browser = bottomPanel(url);
        JPanel topped = topPanel(browser, url);

        bodyPanel.add(topped);
        bodyPanel.add(browser.getComponent());

        ContentFactory contentFactory = ContentFactory.getInstance();
        Content content = contentFactory.createContent(bodyPanel, displayName, true);
        toolWindow.getContentManager().addContent(content);

        return bodyPanel;
    }


    ///导航按键
    public JPanel topPanel(JBCefBrowser jbCefBrowser, String url) {

        //水平方向排列，并且从左往右排
        JPanel rowView = new JPanel(new FlowLayout(FlowLayout.LEFT));

        // 主页按键
        JButton homeBtn = new JButton("主题页");
        rowView.add(homeBtn);
        homeBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jbCefBrowser.loadURL(url);
            }
        });

        // 刷新按键
        JButton refreshBtn = new JButton("刷新");
        rowView.add(refreshBtn);
        refreshBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CefBrowser browser = jbCefBrowser.getCefBrowser();
                browser.reload();
            }
        });

        // 返回按键
        JButton backBtn = new JButton("返回");
        rowView.add(backBtn);
        backBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                CefBrowser browser = jbCefBrowser.getCefBrowser();
                if (browser.canGoBack()) {
                    browser.goBack();
                }
            }
        });

        // 输入框
        JTextField textField = new JTextField(); // 创建一个单行输入框
        textField.setText(url);
        textField.setEditable(true); // 设置输入框允许编辑
        textField.setColumns(20); // 设置输入框的长度为11个字符
        rowView.add(textField); // 在面板上添加单行输入框
        textField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 在这里写下你希望在回车后执行的代码
                String tfUrl = textField.getText();

                System.out.println(tfUrl);
                CefBrowser browser = jbCefBrowser.getCefBrowser();
                browser.loadURL(tfUrl);
            }
        });

        return rowView;
    }

    // 浏览器
    public JBCefBrowser bottomPanel(String url) {
        JBCefBrowser myBrowser = new JBCefBrowser(url);
        return myBrowser;
    }

}



